/**
 * 审计日志 Repository
 * 负责审计日志的数据库操作
 */

import { resultFailure, resultSuccess } from '@utils/resultFormatter'
import { prisma } from './index.repo'

/**
 * 创建审计日志
 */
export const createAuditLog = async (data: {
    actorId: string
    actorType: 'Admin' | 'Member'
    action: string
    targetType: string
    targetId: string
    metadata?: any
}) => {
    try {
        const result = await prisma.auditLog.create({
            data: {
                actorId: data.actorId,
                actorType: data.actorType,
                action: data.action,
                targetType: data.targetType,
                targetId: data.targetId,
                metadata: data.metadata || {}
            }
        })

        return resultSuccess(true, 201, result, 'Audit log created successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

/**
 * 查询审计日志列表
 */
export const readAuditLogList = async (
    where: {
        actorId?: string
        actorType?: string
        action?: string
        targetType?: string
        targetId?: string
        startDate?: Date
        endDate?: Date
    },
    offset: number = 0,
    limit: number = 20
) => {
    try {
        const whereClause: any = {}

        if (where.actorId) {
            whereClause.actorId = where.actorId
        }
        if (where.actorType) {
            whereClause.actorType = where.actorType
        }
        if (where.action) {
            whereClause.action = where.action
        }
        if (where.targetType) {
            whereClause.targetType = where.targetType
        }
        if (where.targetId) {
            whereClause.targetId = where.targetId
        }
        if (where.startDate || where.endDate) {
            whereClause.createdAt = {}
            if (where.startDate) {
                whereClause.createdAt.gte = where.startDate
            }
            if (where.endDate) {
                whereClause.createdAt.lte = where.endDate
            }
        }

        const [result, total] = await Promise.all([
            prisma.auditLog.findMany({
                where: whereClause,
                skip: offset,
                take: limit,
                orderBy: {
                    createdAt: 'desc'
                }
            }),
            prisma.auditLog.count({
                where: whereClause
            })
        ])

        return resultSuccess(true, 200, { result, total }, 'Audit logs retrieved successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}

/**
 * 查询特定目标的审计日志
 */
export const readTargetAuditLogs = async (targetType: string, targetId: string, limit: number = 50) => {
    try {
        const result = await prisma.auditLog.findMany({
            where: {
                targetType,
                targetId
            },
            take: limit,
            orderBy: {
                createdAt: 'desc'
            }
        })

        return resultSuccess(true, 200, result, 'Target audit logs retrieved successfully')
    } catch (err) {
        return resultFailure(false, 500, String(err))
    }
}
